package com.ftg.learn.chapter8.pattern.myreflech;

import com.ftg.learn.chapter8.isa.Animal;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 狮子类
 * <p>
 * Class  : 类的关联
 * Method  : 方法
 * field   : 字段
 *
 * @author kangjx
 * @version 1.0
 * @date 2023/8/8 上午10:11
 */
public class HomeWork01Factory {

    public static Lion getLionInstance(String name, String color) {
        //反射
//        Class cttt = //1 类。class  类名 a = new 类名（）a.getClass()  Class.forName("类地址")
        Class<Lion> l1 = Lion.class;
        Lion mylion = null;
        try {
            mylion = l1.newInstance();
//            Lion mylion = new Lion();


            //l1里面的字段 名称 name
//            Field f1 = l1.getDeclaredField("name");
//            f1.set(mylion, name);
//            Field f2 = l1.getDeclaredField("color");
//            f2.set(mylion, color);

            //反射的技术给 name color
            Method setNameMethod = l1.getDeclaredMethod("setName", String.class);
            setNameMethod.invoke(mylion, name);

//            mylion.setName(name);
            Method setColorMethod = l1.getDeclaredMethod("setColor", String.class);
            setColorMethod.invoke(mylion, color);


        } catch (InstantiationException | IllegalAccessException | NoSuchMethodException |
                 InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        return mylion;
    }


    public static Lion question01011(String name, String color) throws IllegalAccessException, InstantiationException, IntrospectionException, InvocationTargetException {

        Class c1 = Lion.class;
        Object obj = c1.newInstance();

        //字段
        Field[] fields = c1.getDeclaredFields();
        for (Field field : fields) {
            PropertyDescriptor propertyDescriptor = new PropertyDescriptor(field.getName(), c1);
            Method getNameMethod = propertyDescriptor.getReadMethod();
            Method setNameMethod = propertyDescriptor.getWriteMethod();

            //通过setName方法 来name赋値
            setNameMethod.invoke(obj, field.getName().equals("name") ? name : color);
        }
        return (Lion) obj;
    }

    //工厂就是把 创建的细节封装起来，不让使用者知道

    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException, InstantiationException, IntrospectionException {

        String[][] lionsInfo = {
            {"辛巴", "红色"},
            {"辛巴1", "红色"},
            {"辛巴2", "红色"},
            {"辛巴3", "红色"},
            {"辛巴4", "红色"},
            {"辛巴5", "红色"},
            {"辛巴6", "红色"},
            {"辛巴7", "红色"},
            {"辛巴8", "红色"},
            {"辛巴9", "红色"},
            {"辛巴10", "红色"},
            {"辛巴11", "红色"}
        };

        Lion[] lions = new Lion[lionsInfo.length];
        int x = 0;
        for (String[] strings : lionsInfo) {
            Lion l = HomeWork01Factory.getLionInstance(strings[0], strings[1]);
            lions[x++] = l;
        }
    }

}
